//
//  MCPaySelectedLotteryTableViewController.m
//  TLYL
//
//  Created by miaocai on 2017/6/14.
//  Copyright © 2017年 TLYL01. All rights reserved.
//

#import "MCPaySelectedLotteryTableViewController.h"
#import "MCPaySelectedLotteryTableViewCell.h"
#import "MCPaySelectedLotteryHeaderView.h"
#import "MCPaySelectedLotteryFooterView.h"
#import "MCMineCellModel.h"
#import "MCChaseNumberViewController.h"
#import "MCPaySelectedLotteryModel.h"
#import "MCMathUnits.h"
#import "MCMMCViewController.h"
#import "MCMMCPresentationController.h"
#import "MCMSPopView.h"
#define ApplicationNavigationBarBackGauge 7 //导航栏图片距屏幕边缘间距
@interface MCPaySelectedLotteryTableViewController ()
<
UITableViewDelegate,
UITableViewDataSource,
MCPaySelectedLotteryFooterDelegate
>
@property(nonatomic, strong)MCPaySelectedLotteryHeaderView *view_header;
@property(nonatomic, strong)MCPaySelectedLotteryFooterView *view_footer;
@property(nonatomic, strong)UITableView *tableView;
/**Section模型数组*/
@property(nonatomic, strong)NSMutableArray*marr_Section;
/**数据源*/
@property(nonatomic, strong)MCPaySLBaseModel*model;


@end

@implementation MCPaySelectedLotteryTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    [self setProperty];
    
    [self setNav];
    
    [self createUI];
    
    [self loadData];
    
//    [MBProgressHUD showError:[NSString stringWithFormat:@"您选择的玩法为单挑，最高奖金为%@元", ]];
}


-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}
#pragma mark==================setProperty======================
-(void)setProperty{
    self.view.backgroundColor=[UIColor whiteColor];
    
    
    _marr_Section= [[NSMutableArray alloc]init];
    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
        self.extendedLayoutIncludesOpaqueBars = NO;
        self.automaticallyAdjustsScrollViewInsets = NO;
        self.modalPresentationCapturesStatusBarAppearance = NO;
        self.navigationController.navigationBar.clipsToBounds = NO;
    }
    MCPaySLBaseModel * model=[MCPaySLBaseModel sharedMCPaySLBaseModel];
    _model=model;

    self.title=_model.czName;
}
#pragma mark ====================设置导航栏========================
-(void)setNav{
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, MC_REALVALUE(28), MC_REALVALUE(28));
    [rightBtn addTarget:self action:@selector(action_rightBtn) forControlEvents:UIControlEventTouchUpInside];
    [rightBtn setImage:[UIImage imageNamed:@"Navigation_delete_selected"] forState:UIControlStateNormal];
    UIBarButtonItem *rewardItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceItem.width = -ApplicationNavigationBarBackGauge;
    self.navigationItem.rightBarButtonItems = @[spaceItem,rewardItem];
    
}

#pragma mark-清空购彩篮
-(void)action_rightBtn{
    [_marr_Section removeAllObjects];
    [_model removeDataSource];
    [_tableView reloadData];
    _view_footer.dataSource=_model;
}

#pragma mark==================createUI======================
-(void)createUI{
    
    _view_header=[[MCPaySelectedLotteryHeaderView alloc]init];
    [self.view addSubview:_view_header];
    _view_header.frame=CGRectMake(0, 0, G_SCREENWIDTH, [MCPaySelectedLotteryHeaderView computeHeight:nil]);
    
    _view_footer=[[MCPaySelectedLotteryFooterView alloc]init];
    [self.view addSubview:_view_footer];
    _view_footer.delegate=self;
    __block MCPaySelectedLotteryTableViewController *weakSelf = self;
    Type_CZ type=[_model.czName  rangeOfString:@"秒秒彩"].location !=NSNotFound?MC_MMC:MC_Other;
    int Height_footer=[MCPaySelectedLotteryFooterView computeHeight:type];
    _view_footer.frame=CGRectMake(0, G_SCREENHEIGHT-Height_footer-64, G_SCREENHEIGHT, Height_footer);
    [_view_footer relayOutWithType:type];
    _view_footer.block=^(NSInteger t){
#pragma mark-点击追号
        if (t==1001) {
            
            MCChaseNumberViewController *chsaseNumberController = [[MCChaseNumberViewController alloc] init];
            chsaseNumberController.dataSourceModel = weakSelf.model;
            [weakSelf.navigationController pushViewController:chsaseNumberController animated:YES];
            
#pragma mark-点击付款
        }else if(t==1002){
            NSString * tip=[NSString stringWithFormat:@"您一共消费%@元",weakSelf.model.payMoney];

            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确认付款" message:tip preferredStyle:UIAlertControllerStyleAlert];
            // 添加按钮
            __weak typeof(alert) weakAlert = alert;
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                NSLog(@"点击了确定按钮--%@-%@", [weakAlert.textFields.firstObject text], [weakAlert.textFields.lastObject text]);
            }]];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                NSLog(@"点击了取消按钮");
            }]];
            [weakSelf presentViewController:alert animated:YES completion:nil];
        }
    };
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLineEtched;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_view_header.mas_bottom).offset(0);
        make.left.and.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view.mas_bottom).offset(-[MCPaySelectedLotteryFooterView computeHeight:type]);
    }];
    
    
    
    
}

#pragma mark==================loadData======================
-(void)loadData{
    
    [self create_SectionMarr];
    
    _view_footer.dataSource=_model;
    _view_header.dataSource=_model;
    
    
}

-(void)create_SectionMarr{
    [_marr_Section removeAllObjects];
    NSMutableArray * marr_Model=[[NSMutableArray alloc]init];
    for(int i=0 ;i<_model.dataSource.count; i++){
        CellModel* model =[[CellModel alloc]init];
        model.reuseIdentifier = NSStringFromClass([MCPaySelectedLotteryTableViewCell class]);
        model.className=NSStringFromClass([MCPaySelectedLotteryTableViewCell class]);
        model.height = [MCPaySelectedLotteryTableViewCell computeHeight:nil];
        model.selectionStyle=UITableViewCellSelectionStyleNone;
        model.accessoryType=UITableViewCellAccessoryNone;
        /*
         * 传递参数
         */
        model.userInfo = _model.dataSource[i];
        [marr_Model addObject:model];
    }
    SectionModel *model0=[SectionModel sectionModelWithTitle:@"" cells:marr_Model];
    model0.headerhHeight=0.0001;
    model0.footerHeight=0.0001;
    [_marr_Section addObject:model0];
    [_tableView reloadData];
}
#pragma mark tableView 代理相关
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _marr_Section.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.cells.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.headerhHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    SectionModel *sm = _marr_Section[section];
    return sm.footerHeight;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SectionModel *sm = _marr_Section[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    
    return cm.height;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    SectionModel *sm = _marr_Section[indexPath.section];
    CellModel *cm = sm.cells[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cm.reuseIdentifier];
    if (!cell) {
        cell = [[NSClassFromString(cm.className) alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cm.reuseIdentifier];
    }
    cell.selectionStyle = cm.selectionStyle;
    
    if ([cm.className isEqualToString:NSStringFromClass([MCPaySelectedLotteryTableViewCell class])]) {
        
        MCPaySelectedLotteryTableViewCell *ex_cell=(MCPaySelectedLotteryTableViewCell *)cell;
        ex_cell.dataSource=cm.userInfo;
        __block MCPaySelectedLotteryTableViewController *weakSelf = self;
        ex_cell.block=^(){
            
            
//            [weakSelf.model.dataSource removeObjectAtIndex:indexPath.row];
            MCPaySelectedCellModel * dModel= weakSelf.model.dataSource[indexPath.row];
            [weakSelf.model  deleteDataSourceWithModel:dModel];
            weakSelf.model=[MCPaySLBaseModel sharedMCPaySLBaseModel];
            [weakSelf create_SectionMarr];
            [weakSelf.tableView reloadData];
            weakSelf.view_footer.dataSource=weakSelf.model;
            weakSelf.view_header.dataSource=weakSelf.model;
            
        };
        
    }
    
    return cell;
}


#pragma mark-立即开奖
-(void)MCPaySelectedLotteryFooter_RunLottery{
    MCMMCViewController *VC = [[MCMMCViewController alloc]init];    
    MCMMCPresentationController *PC = [[MCMMCPresentationController alloc] initWithPresentedViewController:VC presentingViewController:self];
    VC.transitioningDelegate = PC;
    [self presentViewController:VC animated:YES completion:NULL];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    MCMSPopView *popView = [MCMSPopView alertInstance];
    [popView.dataArray removeAllObjects];
    popView.dataArray=nil;
    [popView hideModelWindow];
}
@end

















